package com.weiyin.examination.activity;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.weiyin.examination.R;
import com.weiyin.examination.http.RetrofitClient;
import com.weiyin.examination.util.LayoutHelper;
import com.weiyin.examination.view.RoundProgressBar;

public class AudioTestActivity extends BaseActivity implements View.OnClickListener {
    private TextView audioYes, audioNo, audioContent;
    private ImageView image;

    private String mp3Url = "/test/test.mp3";

    private MediaPlayer mediaPlayer;
    private Handler mHandler = new Handler();

    private RoundProgressBar course1LayoutProgressBar;

    private AnimationDrawable animationDrawable;
    private ImageView course1LayoutPlay;
    private RelativeLayout relativeLayout,image_RL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio_test);

        mediaPlayer = new MediaPlayer();
        LayoutHelper layoutHelper = new LayoutHelper(this);
        layoutHelper.scaleView(findViewById(R.id.audio_layout));
        initView();

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mediaPlayerPlay();
            }
        }, 200);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
        mediaPlayerStop();
    }

    private void initView() {
        image_RL = (RelativeLayout) findViewById(R.id.image_RL);
        relativeLayout = (RelativeLayout) findViewById(R.id.audioRlPlay);
        course1LayoutPlay = (ImageView) findViewById(R.id.audioImagePlay);
        course1LayoutProgressBar = (RoundProgressBar) findViewById(R.id.audioProgressBar);
        audioContent = (TextView) findViewById(R.id.audioContent);
        audioYes = (TextView) findViewById(R.id.audioYes);
        audioNo = (TextView) findViewById(R.id.audioNo);
        image = (ImageView) findViewById(R.id.image);
        audioContent.setText("你是否可以清晰分辨播放的节奏音频？");
        image_RL.setVisibility(View.GONE);

        audioNo.setOnClickListener(this);
        audioYes.setOnClickListener(this);
        relativeLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.audioRlPlay:
                mediaPlayerPlay();
                break;
            case R.id.audioYes:
                mediaPlayerStop();
                startActivity(new Intent(AudioTestActivity.this, RecordActivity.class));
                finish();
                break;
            case R.id.audioNo:
                mediaPlayerStop();
                startActivity(new Intent(AudioTestActivity.this, ErrorActivity.class));
                finish();
                break;
        }
    }

    private void mediaPlayerPlay() {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(RetrofitClient.BASE_URL + mp3Url);
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                    course1LayoutProgressBar.setMax(mediaPlayer.getDuration());
                    mHandler.post(updateThread);
                    course1LayoutPlay.setImageResource(R.drawable.audio_pause_bg);
                    animationDrawable = new AnimationDrawable();
                    animationDrawable = (AnimationDrawable) course1LayoutPlay.getDrawable();
                    animationDrawable.start();
                }
            });

            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    try {
                        mp.reset();
                        mHandler.removeCallbacks(updateThread);
                        course1LayoutPlay.setImageResource(R.drawable.audio_play);
                        course1LayoutProgressBar.setProgress(0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void mediaPlayerStop() {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
        }
        if (mHandler != null) {
            mHandler.removeCallbacks(updateThread);
        }
        if (animationDrawable != null) {
            animationDrawable.stop();
            course1LayoutPlay.setImageResource(R.drawable.audio_play);
        }
        if (course1LayoutProgressBar != null) {
            course1LayoutProgressBar.setProgress(0);
        }
    }

    /**
     * update进度条
     */
    private Runnable updateThread = new Runnable() {
        @Override
        public void run() {
            try {
                if (mediaPlayer != null) {
                    if (mediaPlayer.getCurrentPosition() == mediaPlayer.getDuration()) {
                        return;
                    }
                    course1LayoutProgressBar.setProgress(mediaPlayer.getCurrentPosition());
                }
                mHandler.removeCallbacks(updateThread);
                mHandler.postDelayed(updateThread, 50);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
}
